package com.design.yjx.proxy.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author yuanjianxin
 * @date 2017/8/23 16:27
 */
public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {
    private State state;
    private int count;
    transient private String location;

    private State hasQuarterState;
    private State noQuarterState;
    private State soldState;
    private State soldOutState;
    private State winnerState;

    public GumballMachine(int count,String location) throws RemoteException {
        this.count = count;
        this.location = location;
        hasQuarterState = new HasQuarterState(this);
        noQuarterState = new NoQuarterState(this);
        soldState = new SoldState(this);
        soldOutState = new SoldOutState(this);
        winnerState = new WinnerState(this);
        if (count > 0) {
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }

    public void releaseBall() {
        System.out.println("发糖");
        if (count != 0) {
            count--;
        }
    }

    public void refill(int count) {
        this.count += count;
        state.refill();
    }

    public void insertQuarter() {
        state.insertQuarter();
    }

    public void ejectQuarter() {
        state.ejectQuarter();
    }

    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public String getLocation() {
        return location;
    }

    @Override
    public String toString() {
        return "当前状态：" + state + ",剩余糖果数量：" + count;
    }
}
